from flask import jsonify
from backend.utils import read_json_file, json_to_dataframe


def get_yearly_counts_api(app):
    @app.route('/api/movie/yearly_counts')
    def get_yearly_counts():
        input_file = '../output.json'
        json_data = read_json_file(input_file)
        df = json_to_dataframe(json_data)

        df = df[df['issue_date'].isin([str(i) for i in range(1900, 2025)])]
        yearly_counts = df['issue_date'].value_counts().sort_index()
        return jsonify(yearly_counts.to_dict())
